import tornado
import tornado.web
import tornado.ioloop
import os
from tornado.ioloop import IOLoop
from tornado import gen
from md5check import md5check

dbclient = md5check("db/user.db")

settings = {
	'debug':True,
	'autoreload':True,
	'cookie_secret':'hello world',
	'db':dbclient,
	"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
class BaseHandler(tornado.web.RequestHandler,md5check):
	

	def get_cookie_user(self):
		user =  (self.get_cookie("user"),self.get_cookie('pass'))
		print user
		return user
	def prepare(self):
		self.db = self.settings['db']


class LoginHandler(BaseHandler):

	@tornado.web.asynchronous
	def post(self):
		login_info = self.get_argument("user")
		passwd = self.get_argument("pass")
		passwd = str(passwd).encode("utf8")
		self.user_geted = login_info
		self.passwd_geted = passwd
		self.set_cookie("user",login_info)
		self.set_cookie("pass",passwd)
		if self.db.check(login_info,passwd):
			self.render("logined.html",user=login_info,info=self.db.getinfo(login_info),back="no")
		else:
			self.render("logined.html",user="your pass is wrong or no such user",info = "5 s return",back="ok")
		print login_info
	def get(self):
		self.render("register.html",post_page="/index")
	def get_input_user(self):
		self.user =(self.get_argument("user"),self.get_argument('pass'))
		print user

class IndexHandler(BaseHandler):

	def get(self):
		print "hello"
		usr,pwd=self.get_cookie_user();
		if self.db.check(usr,pwd):
			self.render("logined.html",user=usr,info=self.db.getinfo(usr),back="no")
		else :
			self.render("before.html")
	def get_input_user(self):
			self.user =  (self.get_argument("user"),self.get_argument('pass'))
			print user


class MainHandler(BaseHandler):

	def get(self):
		if self.get_cookie_user()[0]==None:
			print "no cookie"
			self.redirect("/")
		else:
			usr,pwd= self.get_cookie_user()
			print usr,pwd
			if self.db.sqlcheck(usr):
				self.set_cookie("user","")
				self.set_cookie("pass","")
				self.render("logined.html",user="don't try to inject",info= "none",back="ok")
			else:
				if self.db.check(usr,pwd):
					self.render("logined.html",user=usr,info = self.db.getinfo(usr),back="no")
				else :
					self.set_cookie("user","")
					self.set_cookie("pass","")
					self.render("logined.html",user="your pass is wrong or no such user",info = "none",back="ok")
class ExitHandler(BaseHandler):
	"""docstring for Exit"""
	def get(self):
		self.set_cookie("user","")
		self.set_cookie("pass","")
		self.render("logined.html",user="exit in 5s",info = "exiting",back="ok")
class RegistHandler(BaseHandler):
	def get(self):
		self.render("register.html",back = "welcome ",info="regist first")
	def post(self):
		username = self.get_argument("login-name")
		password = self.get_argument("login-pass")
		info = self.get_argument("login-info")
		if username=="":
			self.render("register.html",back = "username can not be empty!!",info="tips")
		else:
			if self.db.insert_user(username,self.db.encry(password),info):
				self.render("register.html",back = "seccess!!!!",info="tips")
			else:
				self.render("register.html",back = "username has been used!!",info="tips")
		
appication = tornado.web.Application([
		(r'/',IndexHandler),
		(r'/login',LoginHandler),
		(r'/main',MainHandler),
		(r'/exit',ExitHandler),
		(r'/regist',RegistHandler),
	],**settings)

if __name__ == "__main__":
	appication.listen(8001)
	tornado.ioloop.IOLoop.instance().start()